<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        var myobj = {
            name: 'hbxi.jr',
            age: 22,
            yell: function () {
                alert(`我是${this.name}`)//需要使用反引号才能解析
            },
            1: 2
        }

        myobj.yell()
        //以字符串形式取值
        console.log('age:' + myobj['age']);//22
        // console.log('age:'+myobj[age]);//报错
        console.log('age:' + myobj['1']);//2
        console.log('age:' + myobj[1]);//2

        function showProps(obj, objName) {
            var result = "";
            for (var i in obj) {//使用for..in遍历对象
                if (obj.hasOwnProperty(i)) {
                    result += objName + "." + i + " = " + obj[i] + "\n";
                }
            }
            return result;
        }
        console.log(showProps(myobj, 'me'))

        console.log(Object.keys(myobj))//返回键的数组  
        for (i in Object.keys(myobj)) {
            console.log(myobj[Object.keys(myobj)[i]])
        }

        console.log(Object.getOwnPropertyNames(myobj))//返回对象 o 自身包含（不包括原型中）的所有属性 (无论是否可枚举) 的名称的数组

        function standuser(name, age) {//创建构造函数，用于构造对象
            this.name = name;
            this.age = age
            this.introduce = function () {
                console.log(`我的名字是${this.name},年龄${this.age}岁`)
            }
        }
        var killerQueen = new standuser('吉良吉影', 33)
        var crazyDiamond = new standuser('东方仗助', 16)
        killerQueen.introduce()
        crazyDiamond.introduce()

        var standuser = {
            name: undefined,
            age: undefined,
            introduce() {
                console.log(`我的名字是${this.name},年龄${this.age}岁`)
            },
        }
        var goledenExperience = Object.create(standuser)//使用create方法创建对象
        goledenExperience.name = 'GIOGIO'
        goledenExperience.age = 15
        goledenExperience.introduce()

        //get和set将对象属性与函数绑定
        //get在获取属性值时执行函数
        //set在给属性赋值时执行函数
        function Character(config) {
            this.name = config.name
            this.atk = config.atk
            this.def = config.def
            this.lp = config.lp
            this.mgc = config.mgc
        }
        Object.defineProperties(Character.prototype, {
            'hp': {
                get: function () {
                    return `${this.name}生命值还剩下${this.lp}`
                },
                set: function (lp) {
                    this.lp = lp
                    if (this.lp <= 0) {
                        console.log(`${this.name}战斗不能`)
                    } else {
                        console.log(this.hp)
                    }
                }
            },
            'attack': {
                value: function(target) {
                    var dmg = this.atk - target.def
                    target.hp = target.lp - dmg
                }
            }
        })

        var Issac = new Character({
            name: 'Issac',
            atk: 10,
            def: 5,
            lp: 50,
            mgc: 0,
        })
        var Caine = new Character({
            name: 'Caine',
            atk: 15,
            def: 2,
            lp: 30,
            mgc: 0,
        })
        Issac.attack(Caine)
        Issac.attack(Caine)
        Issac.attack(Caine)
        Issac.attack(Caine)
        Issac.attack(Caine)


    </script>

</body>

</html>